//
//  HZMinChartView.m
//  Stock_Health
//
//  Created by MayerF on 2017/7/28.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZMinChartView.h"
#import "MYRadioGroup.h"
#import "HZSummaryView.h"
#import "MinChart.h"
#import "HZWuDangAndPanKouView.h"
#import "HZTopView.h"

#define kSummaryH 90

@interface HZMinChartView()<UIScrollViewDelegate>
@property (nonatomic, strong)MYRadioGroup  *pXuanFuRadio;//悬浮
@property (nonatomic, strong)UIScrollView  *pScrollView;
@property (nonatomic, strong)UIView        *pConstraintsView; //用来约束scrollview的contensize
@property (nonatomic, strong)HZSummaryView *pSummaryView;
@property (nonatomic, strong)MinChart   *pMinChartView;
@property (nonatomic, strong)HZTopView     *pTopView;
@property (nonatomic, strong)HZWuDangAndPanKouView *pWuDangAndPanKou;

@property (nonatomic, assign)NSInteger nStyle;
@property (nonatomic, assign)NSInteger nXuanFuState;
@end

@implementation HZMinChartView
- (UIView *)p_loadTopView
{
    if (!_pTopView) {
        _pTopView = [HZTopView new];
        _pTopView.parent = self;
        [_pConstraintsView addSubview:_pTopView];
    }
    [_pTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pMinChartView.mas_bottom);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.mas_equalTo(_pConstraintsView);
    }];
    
    return _pTopView;
}
- (UIView *)p_loadWuDangAndPankouView
{
    if (!_pWuDangAndPanKou) {
        _pWuDangAndPanKou = [HZWuDangAndPanKouView new];
        [_pConstraintsView addSubview:_pWuDangAndPanKou];
    }
    [_pWuDangAndPanKou mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pMinChartView.mas_bottom);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.bottom.mas_equalTo(_pConstraintsView);
        make.height.mas_equalTo(150);
    }];
    
    return _pWuDangAndPanKou;
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pScrollView = [UIScrollView new];
    _pConstraintsView = [UIView new];
    _pConstraintsView.backgroundColor = [HZCommonTool getThemeColor];
    _pSummaryView = [HZSummaryView new];
    _pSummaryView.parent = self;
    _pMinChartView = [MinChart new];
    _pMinChartView.parent = self;
    _pScrollView.delegate = self;
    [_pMinChartView setMode:MIN_SHOW_MODE_NORMAL];
    
    
    [self addSubview:_pScrollView];
    [_pScrollView addSubview:_pConstraintsView];
    [_pConstraintsView addSubview:_pSummaryView];
    [_pConstraintsView addSubview:_pMinChartView];
//    [self p_loadWuDangAndPankouView];
    [self p_initConstraints];
    
}
- (void)p_initConstraints
{
    [_pScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    [_pConstraintsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pScrollView);
        make.width.mas_equalTo(self);
//        make.height.mas_equalTo(self);
    }];
    [_pSummaryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pConstraintsView);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.mas_equalTo(_pConstraintsView);
        make.height.mas_equalTo(kSummaryH);
    }];
    [_pMinChartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pSummaryView.mas_bottom).offset(5);
        make.left.mas_equalTo(_pConstraintsView);
        make.right.mas_equalTo(_pConstraintsView);
//        make.height.mas_equalTo(self).multipliedBy(0.4647);
    }];
    
//    [self.superview setNeedsLayout];
//    [self.superview layoutIfNeeded];
}

- (void)bindCodeInfo:(HZOwnedStockModel *)codeInfo
{
    _nStyle = codeInfo.nModel_Style;
    if (codeInfo.nModel_Style == 1) {
        [self p_loadTopView];
        [_pConstraintsView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(_pScrollView);
            make.width.mas_equalTo(self);
            make.bottom.mas_equalTo(_pTopView);
            
        }];
        [_pMinChartView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pSummaryView.mas_bottom).offset(5);
            make.left.mas_equalTo(_pConstraintsView);
            make.right.mas_equalTo(_pConstraintsView);
            make.height.mas_equalTo(250);
        }];
    }else {
        [self p_loadWuDangAndPankouView];
        [_pConstraintsView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(_pScrollView);
            make.width.mas_equalTo(self);
            make.height.mas_equalTo(self);
//            make.bottom.mas_equalTo(_pWuDangAndPanKou);
        }];
        [_pMinChartView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pSummaryView.mas_bottom).offset(5);
            make.left.mas_equalTo(_pConstraintsView);
            make.right.mas_equalTo(_pConstraintsView);
        }];
    }
    [self.superview setNeedsLayout];
    [self.superview layoutIfNeeded];
}
- (void)updateNewData:(HZMinPriceModel *)data
{
    [_pSummaryView updateNewData:data from:KFromMinType];
    [_pWuDangAndPanKou updateNewData:data];
}
- (void)updateMinData
{
    [_pMinChartView updateNewData];
}
- (void)updateDetailData:(NSArray *)detailData
{
    [_pWuDangAndPanKou updateDetialData:detailData];
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    CGFloat fChartViewMaxY = CGRectGetMaxY(_pMinChartView.frame);
    DLog(@"===%f====%f\n",point.y,fChartViewMaxY);
    if (point.y > fChartViewMaxY && _pTopView) {
        self.pXuanFuRadio.hidden = NO;
        [self.pXuanFuRadio setSelectedButton:0];
    }else{
        self.pXuanFuRadio.hidden = YES;
    }
}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_setLandscapeScreen_Action:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
            
        default:
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
            break;
    }
}
- (void)p_sendInfoWithWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter
{
    if (![objc isKindOfClass:[NSNumber class]]) {
        return;
    }
    kHZApiType nApiType = [objc integerValue];
    switch (nApiType) {
        case kHZApi_IndexZhangFuTop_Type:
        case kHZApi_IndexDieFuTop_Type:
        case kHZApi_IndexHuanShouTop_Type:
        {
            if (_nStyle == 1) {
                [_pTopView downToChildWithObjc:objc withType:type withParamter:paramter];
            }
        }
            break;
            
        default:
            break;
    }
}

@end
